package com.xyzwps.benchmarks.json;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;

public class JacksonUtils implements JsonUtils {
    private static final ObjectMapper om = new ObjectMapper();

    static {
        om.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, true);
    }

    public String stringify(Object obj) {
        try {
            return om.writeValueAsString(obj);
        } catch (JsonProcessingException e) {
            throw new IllegalArgumentException("对象JSON序列化失败", e);
        }
    }

    public <T> T parse(String s, Class<T> c) {
        try {
            return om.readValue(s, c);
        } catch (JsonProcessingException e) {
            throw new IllegalArgumentException("对象JSON序列化失败", e);
        }
    }

    public static final JacksonUtils INSTANCE = new JacksonUtils();
}
